
const Controller = require('egg').Controller;

class HomeController extends Controller {
  async getList() {
    const { ctx } = this;
    const data = await ctx.service.list.getList()
    ctx.body= data
  }
  async addList(){
    const {ctx}=this;
    const data = await ctx.service.list.addList()
    if(data.affectedRows===1){
      ctx.body={
        message:"添加成功",
        data:{
          id:data.insertId
        },
        code:1
      }
    }else{
      ctx.body={
        message:"添加失败",
        code:-1
      }
    }
  }
 
  async editList(){
    const {ctx}=this;
    const data = await ctx.service.list.editList()
    if(data.affectedRows===1){
      ctx.body={
        message:"编辑成功",
        code:1
      }
    }else{
      ctx.body={
        message:"编辑失败",
        code:-1
      }
    }
  }
  async delete() {
    const { ctx } = this;
    let res = await ctx.service.list.delete()
    ctx.body =res;
  }
  async login(){
    const {ctx}=this;
    const data = await ctx.service.user.login()
    if(data.token){
      ctx.body={
        code:1,
        data
      }
    }else{
      ctx.body={
        code:-1,
        message:"登陆成功"
      }
    }
  }
  //注册
  async regisert(){
    const {ctx}=this;
    const data = await ctx.service.user.regisert()
    if(data.affectedRows===1){
      ctx.body={
        code:1,
        message:"注册成功"
      }
    }else{
      ctx.body={
        code:-1,
        message:"注册失败"
      }
    }
  }
}

module.exports = HomeController;
